package com.coffee.order.service.sku.impl;

import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import com.coffee.order.api.sku.query.SkuOrderDetailQuery;
import com.coffee.order.api.sku.result.SkuOrderDetailResult;
import com.coffee.order.api.sku.service.SkuOrderDetailService;
import com.coffee.order.service.sku.bean.SkuOrderDetail;
import com.coffee.order.service.sku.dao.SkuOrderDetailDao;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class SkuOrderDetailServiceImpl implements SkuOrderDetailService {
	@Resource
	private SkuOrderDetailDao	skuOrderDetailDao;

	@Override
	public Page<SkuOrderDetailResult> getPager(SkuOrderDetailQuery param, PagerInfo pr) {
		Page<SkuOrderDetailResult> pager=new Page<SkuOrderDetailResult>(pr.getStart(), pr.getPageSize());
		Integer count=skuOrderDetailDao.count(param);
		pager.setTotalPage(count);
		if(count>0) {
			List<SkuOrderDetail> list=skuOrderDetailDao.page(param, pr.getStart(), pr.getPageSize());
			pager.setData(DTOUtils.convertList(list, SkuOrderDetailResult.class));
		}
		return pager;
	}
	
	@Override
	public Integer count(SkuOrderDetailQuery param){
		Integer count=skuOrderDetailDao.count(param);
		return count;
	}
	
	@Override
	public SkuOrderDetailResult getDetail(Integer id){
		SkuOrderDetail item=skuOrderDetailDao.findById(id);
		if(item==null){
			return null;
		}
		SkuOrderDetailResult result=new SkuOrderDetailResult();
		BeanUtilExt.copyProperties(result, item);
		
		return result;
	}

	@Override
	public List<SkuOrderDetailResult> getList(SkuOrderDetailQuery param, PagerInfo pr){
		List<SkuOrderDetail> list=skuOrderDetailDao.page(param, pr.getStart(), pr.getPageSize());
		return DTOUtils.convertList(list, SkuOrderDetailResult.class);
	}
	
	@Override
	public List<SkuOrderDetailResult> getList(SkuOrderDetailQuery param){
		List<SkuOrderDetail> list=skuOrderDetailDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuOrderDetailResult.class);
	}
	
	@Override
	public List<SkuOrderDetailResult> getList(){
		SkuOrderDetailQuery param = new SkuOrderDetailQuery();
		List<SkuOrderDetail> list=skuOrderDetailDao.page(param, 0, Integer.MAX_VALUE);
		return DTOUtils.convertList(list, SkuOrderDetailResult.class);
	}


	@Override
	public void delete(Integer id) {
		skuOrderDetailDao.deleteById(id);
	}

	@Override
	public void addOrUpdate(SkuOrderDetailResult result)throws BusinessException {
		SkuOrderDetail item=new SkuOrderDetail();
		BeanUtilExt.copyProperties(item, result);
		if(item.getId()==null) {
			skuOrderDetailDao.insert(item);
		}else {
			SkuOrderDetail tmp=skuOrderDetailDao.findById(item.getId());
			if(tmp==null) {
				throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
			}
			skuOrderDetailDao.updateById(item);
		}
	}
	
	@Override
	public Map<Integer,SkuOrderDetailResult> getItemMap(Set<Integer> idSet){
		Map<Integer, SkuOrderDetailResult> map=new HashMap<Integer, SkuOrderDetailResult>();
		List<SkuOrderDetail> list=skuOrderDetailDao.findByIds(StringUtil.setToList(idSet));
		List<SkuOrderDetailResult> resultList=DTOUtils.convertList(list, SkuOrderDetailResult.class);
		for(SkuOrderDetailResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}
	
	@Override
	public Map<Integer,SkuOrderDetailResult> getItemMap(){
		Map<Integer, SkuOrderDetailResult> map=new HashMap<Integer, SkuOrderDetailResult>();
		SkuOrderDetailQuery param=new SkuOrderDetailQuery();
		List<SkuOrderDetail> list=skuOrderDetailDao.page(param, 0, Integer.MAX_VALUE);
		List<SkuOrderDetailResult> resultList=DTOUtils.convertList(list, SkuOrderDetailResult.class);
		for(SkuOrderDetailResult item:resultList) {
			map.put(item.getId(), item);
		}
		return map;
	}	
	
	
	
}
